package com.ruoyi.iot.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import org.springframework.stereotype.Service;

import com.ruoyi.iot.mapper.ChannelMapper;
import com.ruoyi.iot.mapper.PropertyMapper;
import com.ruoyi.iot.domain.Channel;
import com.ruoyi.iot.domain.Property;
import com.ruoyi.iot.domain.emun.ChannelConstant;
import com.ruoyi.iot.service.IPropertyService;

import jakarta.annotation.Resource;
import org.springframework.util.CollectionUtils;

/**
 * 通道属性Service业务层处理
 * 
 * @author 常康
 * @date 2025-04-03
 */
@Service
public class PropertyServiceImpl implements IPropertyService {

    @Resource
    private PropertyMapper propertyMapper;

    @Resource
    private ChannelMapper channelMapper;


    /**
     * 查询通道属性
     * 
     * @param id 通道属性主键
     * @return 通道属性
     */
    @Override
    public Property selectPropertyById(Long id){
        return propertyMapper.selectPropertyById(id);
    }

    /**
     * 查询通道属性列表
     * 
     * @param property 通道属性
     * @return 通道属性
     */
    @Override
    public List<Property> selectPropertyList(Property property){
        return propertyMapper.selectPropertyList(property);
    }

    /**
     * 新增通道属性
     * 
     * @param property 通道属性
     * @return 结果
     */
    @Override
    public int insertProperty(Property property){
        Channel channel = channelMapper.selectChannelById(property.getChannelId());
        if(selectPropertyByKey(channel.getId(), property.getKey()) != null){
            throw new ServiceException( "属性标识已存在");
        }
        property.setChannelKey(channel.getKey());
        property.setProductKey(channel.getProductKey());
        return propertyMapper.insertProperty(property);
    }

    /**
     * 修改通道属性
     * 
     * @param property 通道属性
     * @return 结果
     */
    @Override
    public int updateProperty(Property property){
        Channel channel = channelMapper.selectChannelById(property.getChannelId());
        Property oldProperty = selectPropertyByKey(channel.getId(), channel.getKey());
        if(oldProperty!= null && !oldProperty.getId().equals(property.getId())){
            throw new ServiceException( "属性标识已存在");
        }
        return propertyMapper.updateProperty(property);
    }

    /**
     * 批量删除通道属性
     * 
     * @param ids 需要删除的通道属性主键
     * @return 结果
     */
    @Override
    public int deletePropertyByIds(Long[] ids){
        return propertyMapper.deletePropertyByIds(ids);
    }

    /**
     * 删除通道属性信息
     * 
     * @param id 通道属性主键
     * @return 结果
     */
    @Override
    public int deletePropertyById(Long id){
        return propertyMapper.deletePropertyById(id);
    }

    @Override
    public Property selectPropertyByKey(Long channelId, String propertyKey) {
        Property property = new Property();
        property.setChannelId(channelId);
        property.setProductKey(propertyKey);
        List<Property> propertyList = selectPropertyList(property);
        if(CollectionUtils.isEmpty(propertyList)){
            return null;
        }
        return propertyList.get(0);
    }

    @Override
    public List<Property> selectUpPropertyList(String channelKey, String ProductKey) {
        Property property = new Property();
        property.setChannelKey(channelKey);
        property.setProductKey(ProductKey);
        property.setDirection(ChannelConstant.UP);
        return selectPropertyList(property);
    }

    @Override
    public List<Property> selectDownPropertyList(String channelKey, String ProductKey) {
        Property property = new Property();
        property.setChannelKey(channelKey);
        property.setProductKey(ProductKey);
        property.setDirection(ChannelConstant.DOWN);
        return selectPropertyList(property);
    }

    @Override
    public List<Property> selectDefaultProperty(String channelKey, String ProductKey) {
        Property property = new Property();
        property.setChannelKey(ChannelConstant.DEFAULT_CHANNEL_KEY);
        property.setProductKey(ProductKey);
        return selectPropertyList(property);
    }


}
